package com.irdstudio.apicenter.console.dm.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.irdstudio.apicenter.frame.core.jdbc.util.ConnectionManager;

/**
 * 获取当前页数总条数等信息
 * 
 * @author TP
 * 
 */
public class FootPageDao {
	/**
	 * 
	 * @param sql
	 *            传入需要统计的sql
	 * @param row页数
	 * @param size条数
	 * @return
	 * @throws SQLException
	 */
	public FootPage querypage(String sql, int row, int size)
			throws SQLException {
		if (row == 0)
			row = 1;
		if (size == 0)
			size = 10;
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		FootPage fp = new FootPage();
		try {

			System.out.println("分页sql的查询语句" + sql);

			ps = conn.prepareStatement("select count(1) as totalCount from "
					+ sql);
			rs = ps.executeQuery();
			int totalCount = 0;
			while (rs.next()) {
				totalCount = rs.getInt("totalCount");
			}

			fp.setFrontpage(row - 1);
			fp.setNextpage(row + 1);
			fp.setPageNo(row);
			fp.setPageSize(size);
			fp.setTotalCount(totalCount);
			fp.setTotalPage(totalCount / size);
			int mod = totalCount % size;
			if (mod != 0) {
				fp.setTotalPage((totalCount / size) + 1);
			} else {
				fp.setTotalPage(totalCount / size);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ps.close();
			rs.close();
			conn.close();

		}
		return fp;

	}

}
